//
//  UserInfoManger.m
//  WeCommunity
//
//  Created by mac on 2017/6/20.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "UserInfoManger.h"
#import "LoginViewController.h"
#import "UserServer.h"
#import "EditTimelineViewController.h"
#import "MineHomeViewController.h"

static UserInfoManger * userInfoManger = nil;

@implementation UserInfoManger

+(UserInfoManger *)shared
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        userInfoManger = [[UserInfoManger alloc] init];
        
        userInfoManger.contact = [ContactModel new];
        
        [userInfoManger addRacSingal];
        
    });
    return userInfoManger;
}

#pragma mark - 验证 SessionToken
-(void)verifySessionToken
{
    [[UserServer shared] verifySessionTokenComplete:^(BOOL available) {
        self.isLogin = available;
    }];
}

-(BOOL)userIsAdmin
{
    if ([UserInfoManger shared].contact.userIsAdmin && self.isLogin) {
        return YES;
    }
    return NO;
}

-(void)reloadUserInfo
{
    if(self.isLogin){
        self.contact = [[UserServer shared] getSelfContact];
    }else{
        self.contact = [ContactModel new];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:USERLOGIN_CHANGE_NOTIKEY object:self.contact];
}

#pragma mark - RAC 观察登录状态
-(void)addRacSingal
{
    @weakify(self);
    [RACObserve(self, isLogin) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self reloadUserInfo];
    }];
}

#pragma mark - 保存头像
-(void)saveHeadImage:(UIImage *)headImage complete:(void(^)(BOOL success,NSString * error))complete
{
    @weakify(self);
    [[UserServer shared] saveHeadImage:headImage complete:^(BOOL success, NSString *error, NSString *imageUrl) {
        @strongify(self);
        if (success) {
            self.contact.headImage = imageUrl;
            
            [[NSNotificationCenter defaultCenter] postNotificationName:USERLOGIN_CHANGE_NOTIKEY object:self.contact];
        }
        complete(success,error);
    }];
}
#pragma mark - 保存昵称
-(void)saveUserNickName:(NSString *)nickName complete:(void(^)(BOOL success,NSString * error))complete
{
    @weakify(self);
    [[UserServer shared] saveUserNickName:nickName complete:^(BOOL success, NSString *error, NSString *nickName) {
        @strongify(self);
        if (success) {
            self.contact.nickName = nickName;
            
            [[NSNotificationCenter defaultCenter] postNotificationName:USERLOGIN_CHANGE_NOTIKEY object:self.contact];
        }
        complete(success,error);
    }];
}

#pragma mark - 保存微信号
-(void)saveUserWXUserName:(NSString *)userName complete:(void(^)(BOOL success,NSString * error))complete
{
    @weakify(self);
    [[UserServer shared] saveUserWXUserName:userName complete:^(BOOL success, NSString *error, NSString *WXUserName) {
        @strongify(self);
        if (success) {
            self.contact.WXUserName = WXUserName;

            [[NSNotificationCenter defaultCenter] postNotificationName:USERLOGIN_CHANGE_NOTIKEY object:self.contact];
        }
        complete(success,error);
    }];
}

#pragma mark - 保存性别
-(void)saveUserSex:(NSString *)sex complete:(void(^)(BOOL success,NSString * error))complete
{
    @weakify(self);
    [[UserServer shared] saveUserSex:sex complete:^(BOOL success, NSString *error, NSString *sex) {
        @strongify(self);
        if (success) {
            self.contact.sex = sex;
            
            [[NSNotificationCenter defaultCenter] postNotificationName:USERLOGIN_CHANGE_NOTIKEY object:self.contact];
        }
        complete(success,error);
    }];
}

#pragma mark - 增减积分   number 负数表示减积分  负数自动记录使用的积分
-(void)addCoins:(NSNumber *)number complete:(void(^)(BOOL success,NSString * error))complete
{
    [[UserServer shared] addCoins:number complete:^(BOOL success, NSString *error, NSNumber *coins) {
        if(success){
            self.contact.coins = coins;
            self.contact.usedCoins = [[AVUser currentUser] objectForKey:@"usedCoins"];
            complete(YES,nil);
            [[NSNotificationCenter defaultCenter] postNotificationName:USERLOGIN_CHANGE_NOTIKEY object:self.contact];

        }else{
            complete(NO,error);
        }
    }];
}






#pragma mark - 去登陆
-(BOOL)needGotoLoginLogic
{
    if (!self.isLogin) {
     
        //相册
        LoginViewController * loginVC = [[LoginViewController alloc] init];
        
        RootNaviViewController * nav = [[RootNaviViewController alloc] initWithRootViewController:loginVC];
        nav.navigationBar.translucent = YES;//设置不透明
        nav.navigationBar.barTintColor = WHITE_COLOR; //导航条颜色
        nav.navigationBar.tintColor = [UIColor blackColor];  //导航条字体颜色
        
        [[UITool getCurrentVC] presentViewController:nav animated:YES completion:nil];
        
        return YES;
    }
    return NO;
}

#pragma mark - 退出登录
-(void)logOutLogic
{
    [[UserServer shared] logOut];
    self.isLogin = NO;
    [self reloadUserInfo];
    
//    [[KWindowViewMgr shared] removeAllKWindowViews];
//    //全部出栈
//    if ([UIControllerCenter shared].rootTabBarController.presentedViewController) {
//        UIViewController * preVC = [UIControllerCenter shared].rootTabBarController.presentedViewController;
//        [preVC dismissViewControllerAnimated:NO completion:nil];
//    }
//    
//    RootNaviViewController * navc = [UIControllerCenter shared].rootTabBarController.selectedViewController;
//    if (navc.viewControllers.count > 1) {
//        UIViewController * subVC = navc.viewControllers.lastObject;
//        [subVC.navigationController popToRootViewControllerAnimated:NO];
//    }
//    
//    [(BaseViewController *)[UITool getCurrentVC] sureAlertWithTitle:nil andMesg:KNSLocal(@"被退出登录")];
    
}

#pragma mark - 是否购买了去除广告
-(BOOL)isRemovedAD{
    
    NSUserDefaults *de = [NSUserDefaults standardUserDefaults];
    if([de objectForKey:IS_REMOVE_ADS_KEY]){
        return [de boolForKey:IS_REMOVE_ADS_KEY];
    }
    
    return NO;
}




@end
